package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjCustomerAuthor;
import com.zb.mapper.ZjCustomerAuthorMapper;
import com.zb.service.ZjCustomerAuthorService;
import com.zb.vo.AuthorVo;
import com.zb.vo.CustomerAuthorVo;
import com.zb.vo.MenuAuthorVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商家权限菜单表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-02-20
 */
@Service
public class ZjCustomerAuthorServiceImpl extends ServiceImpl<ZjCustomerAuthorMapper, ZjCustomerAuthor> implements ZjCustomerAuthorService {
    @Resource
    private ZjCustomerAuthorMapper customerAuthorMapper;

    // 根据商家ID获取所有菜单权限
    @Override
    public List<CustomerAuthorVo> getAllAuthor(Long custAccId) {
        List<ZjCustomerAuthor> authorList = customerAuthorMapper.getAllAuthorByCustAccId(custAccId);
        return authorList.stream().filter(f -> f.getParentAuthorId() == 0).map(author -> {
            CustomerAuthorVo vo = new CustomerAuthorVo();
            BeanUtil.copyProperties(author, vo);
            vo.setStatus(author.getDisabled());
            vo.setAuthorVos(getChildAuthor(vo.getAuthorId(), authorList));
            return vo;
        }).toList();
    }

    public List<CustomerAuthorVo> getChildAuthor(Long parentAuthorId, List<ZjCustomerAuthor> authorList) {
        return authorList.stream().filter(f -> Objects.equals(f.getParentAuthorId(), parentAuthorId)).map(author -> {
            CustomerAuthorVo vo = new CustomerAuthorVo();
            BeanUtil.copyProperties(author, vo);
            vo.setStatus(author.getDisabled());
            vo.setAuthorVos(getChildAuthor(vo.getAuthorId(), authorList));
            return vo;
        }).toList();
    }

    // 根据商家ID获取管理员菜单权限
    @Override
    public List<AuthorVo> getAdminRootByCustAccId(Long custAccId) {
        return customerAuthorMapper.getAdminRootByCustAccId(custAccId);
    }

}
